(ns server.dbmain-test
	(:use clojure.test server.dbmain)
	(:import (java.io File FileInputStream FileOutputStream))
)
	
;;------------------------------------------------------------------------------------------------------------------------------------	
(def *input* "db-1x2.db")
(def *output* "db")

(defn copy-database
	"Makes a copy of the current database, just to make tests"
	[]
	(with-open [
		input-stream  (FileInputStream. (File. *input*))
		output-stream (FileOutputStream. (File. *output*))
		]
		(let [buffer (byte-array 1024)]
			(loop [len (.read input-stream buffer)]
				(if (> len 0)
					(do
						(.write output-stream buffer, 0, len)
						(recur (.read input-stream buffer))
					)
				)
			)
		)
	)
)

;;------------------------------------------------------------------------------------------------------------------------------------
					
(deftest map-a-map-test
	(is (= {:a 2} (map-a-map inc {:a 1})))
	(is (= {:a 4 :b 5} (map-a-map dec {:a 5 :b 6})))
	(is (= {:a "aa.*" :b "ab.*" :c "abc.*" :d ".*"} 
		(map-a-map #(str % ".*") {:a "aa" :b "ab" :c "abc" :d ""})))
)

;;------------------------------------------------------------------------------------------------------------------------------------

(deftest off-first-test
	(is (= "ola" (off-first "hola")))
	(is (= "emana Santa" (off-first "Semana Santa")))
	(is (= "" (off-first "")))
)

;;------------------------------------------------------------------------------------------------------------------------------------	

(deftest matches-criteria-test 
	(is (= true (matches-criteria {:a "just-testing"} {:a ""})))
	(is (= true (matches-criteria {:a "aatesting"} {:a "aa"})))
	(is (= true (matches-criteria 
		{:a "Pete" :b "Martin"} {:a "Pet"  :b "Martin"})))
	(is (= false (matches-criteria {:a "just-testing"} {:a "b"})))
	(is (= false (matches-criteria {:a "aatesting"} {:a "aab"})))
	(is (= false (matches-criteria 
		{:a "Pete" :b "Martin"} {:a "Peter" :b "Martin"})))
)

;;------------------------------------------------------------------------------------------------------------------------------------

(deftest find-in-db-test
	(is (= (reverse (range 29))(find-in-db {:owner "" :date "" :rate "" :smoking "" :size "" :location "" :name ""} "db")))
	(is (= '() (find-in-db {:owner "a" :date "" :rate "" :smoking "" :size "" :location "" :name ""} "db")))
	(is (= '(28 27 26 23 22 21 20 19 13 12 10 4 2 1) (find-in-db {:owner "" :date "" :rate "" :smoking "" :size "4" :location "" :name ""} "db")))
	(is (= '(1 0) (find-in-db {:owner "" :date "" :rate "" :smoking "" :size "" :location "Smallville" :name ""} "db")))
	(is (= '(28 27 22 19 17 16 14 9 7 6 3 1) (find-in-db {:owner "" :date "" :rate "" :smoking "Y" :size "" :location "" :name ""} "db")))
	(is (= (reverse (range 29))(find-in-db {:owner "" :date "" :rate "$" :smoking "" :size "" :location "" :name ""} "db")))
	(is (= '(17 1) (find-in-db {:owner "" :date "" :rate "$150.00" :smoking "" :size "" :location "" :name ""} "db")))
)

;;------------------------------------------------------------------------------------------------------------------------------------

(deftest read-record-test
	(copy-database)
	(is (= {:owner "" :date "2004/11/15" :rate "$150.00" :smoking "Y" :size "4" :location "Smallville" :name "Splendide" :deleted "false"} (read-record "db" 1)))
	(is (= {:owner "" :date "2005/03/23" :rate "$210.00" :smoking "N" :size "2" :location "Smallville" :name "Excelsior" :deleted "false"} (read-record "db" 0)))
	(is (= {:owner "" :date "2003/02/08" :rate "$100.00" :smoking "N" :size "6" :location "EmeraldCity" :name "Grandview" :deleted "false"} (read-record "db" 15)))
)

;;------------------------------------------------------------------------------------------------------------------------------------



(deftest delete-record-test
	(copy-database)
	(delete-record "db" 0)
	(is (= "true" (:deleted (read-record "db" 0))))
	(delete-record "db" 1)
	(is (= "true" (:deleted (read-record "db" 1))))
	(delete-record "db" 2)
	(is (= "true" (:deleted (read-record "db" 2))))
	(delete-record "db" 3)
	(is (= "true" (:deleted (read-record "db" 3))))
)

;;------------------------------------------------------------------------------------------------------------------------------------

(deftest update-record-test
	(copy-database)
	(update-record "db" 5 {:size "5"})
	(is (= "5" (:size (read-record "db" 5))))
	(update-record "db" 6 {:owner "12345678"})
	(is (= "12345678" (:owner (read-record "db" 6))))
	(update-record "db" 7 {:rate "$00.00" :smoking "Y"})
	(is (= "$00.00" (:rate (read-record "db" 7))))
	(update-record "db" 8 {:rate "$00.00" :smoking "Y"})
	(is (= "Y" (:smoking (read-record "db" 8))))
)

;;------------------------------------------------------------------------------------------------------------------------------------

(deftest create-record-test
	(copy-database)
	(is (= {:owner "" :date "" :rate "" :smoking "" :size "" :location "" :name "" :deleted "false"} 
		(read-record "db" (create-record "db" {:owner "" :date "" :rate "" :smoking "" :size "" :location "" :name ""}))))
	(is (= {:owner "1" :date "2001/01/01" :rate "$30.00" :smoking "N" :size "20" :location "Here" :name "Great Place" :deleted "false"} 
		(read-record "db" (create-record "db" {:owner "1" :date "2001/01/01" :rate "$30.00" :smoking "N" :size "20" :location "Here" :name "Great Place"}))))
	(is (= {:owner "a" :date "a" :rate "a" :smoking "a" :size "a" :location "a" :name "a" :deleted "false"} 
		(read-record "db" (create-record "db" {:owner "a" :date "a" :rate "a" :smoking "a" :size "a" :location "a" :name "a" }))))
)
;;------------------------------------------------------------------------------------------------------------------------------------

(do
	(copy-database)
	(run-tests)
	(.delete (File. "db"))
)

;(use :reload 'server.dbmain-test) to run the tests
